package com.rimi.cs.javaweb.controller;

import com.rimi.cs.javaweb.dao.MobileMapper;
import com.rimi.cs.javaweb.dao.OrderDetailMapper;
import com.rimi.cs.javaweb.dao.OrderMapper;
import com.rimi.cs.javaweb.dao.UserMapper;
import com.rimi.cs.javaweb.pojo.Mobile;
import com.rimi.cs.javaweb.pojo.Order;
import com.rimi.cs.javaweb.pojo.OrderDetail;
import com.rimi.cs.javaweb.pojo.User;
import com.rimi.cs.javaweb.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个人信息控制器
 *
 * @author Hayes
 * @date 2018-06-30 08:48
 */
@Controller
public class InfomationController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private MobileMapper mobileMapper;

    /**
     * 根据User实例更新用户
     * @param username 用户名
     * @param gender 性别
     * @param phone 手机号码
     * @param email 邮箱地址
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/information/update", method = RequestMethod.GET)
    public Map updateInformation(String username, String gender, String phone, String email, HttpServletRequest request) {
        Map map = new HashMap(1);
        User user = (User) request.getSession().getAttribute(Constant.USER_INFO);
        if (user != null) {
            user.setUsername(username);
            System.out.println(user.getUserid());
            System.out.println(username+" "+gender+" "+phone+" "+email);
            user.setGender((gender).equals("male")? 0 : 1);
            user.setPhone(phone);
            user.setEmail(email);
            if (userMapper.updateByUser(user)) {
                map.put("updateResult","true");
            } else {
                map.put("updateResult","false");
            }

        } else {
            map.put("updateResult","notexist");
        }
        return map;
    }

    /**
     * 根据用户ID查询订单列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/information/selectOrder")
    public Map  selectOrder(HttpServletRequest request) {
        Map<String,List<Order>> map = new HashMap<>(1);
        User user = (User) request.getSession().getAttribute(Constant.USER_INFO);
        List<Order> orderList = orderMapper.selectByUserId(user.getUserid());
        map.put("orderList",orderList);
        return map;
    }

    /**
     * 根据订单ID查询订单详情
     * @param orderId 订单ID
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/information/selectOrderDetail")
    public Map  selectOrderDetail(Long orderId) {
        Map<String,List<OrderDetail>> map = new HashMap<>(1);
        List<OrderDetail> orderDetailList = orderDetailMapper.selectByOrderId(orderId);
        for (OrderDetail orderDetail : orderDetailList) {
            Mobile mobile = mobileMapper.selectByPrimaryKey(orderDetail.getPhoneid());
            orderDetail.setPhonename(mobile.getPhonename());
        }
        map.put("orderDetailList",orderDetailList);
        return map;
    }

}
